package com.tyk.bookstore.back.platform.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.tyk.bookstore.back.common.core.model.page.PageVo;
import com.tyk.bookstore.back.common.core.model.result.R;
import com.tyk.bookstore.back.platform.model.query.CarouselAddQuery;
import com.tyk.bookstore.back.platform.model.query.CarouselPageQuery;
import com.tyk.bookstore.back.platform.model.query.CarouselUpdateQuery;
import com.tyk.bookstore.back.platform.model.vo.CarouselPageVo;
import com.tyk.bookstore.back.platform.model.vo.CarouselVo;
import com.tyk.bookstore.back.platform.service.CarouselService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Set;

/**
 * @author tyk
 * @description 轮播图管理
 */
@RequestMapping({"api/platform/carousel"})
@Tag(name = "平台管理/轮播图管理", description = "轮播图管理")
@RestController
@RequiredArgsConstructor
public class CarouselController {
    private final CarouselService carouselService;

    @Operation(summary = "查询轮播图详情", description = "查询轮播图详情")
    @SaCheckPermission("platform:carousel:get")
    @GetMapping({"{id}"})
    public R<CarouselVo> getById(
            @Schema(description = "轮播图详情") @NotNull @Min(1)
            @PathVariable Long id
    ) {
        return R.ok(carouselService.getById(id));
    }


    @Operation(summary = "分页查询轮播图", description = "分页查询轮播图")
    @SaCheckPermission("platform:carousel:page")
    @GetMapping({"page"})
    public R<CarouselPageVo> getPage(
            @Validated @ParameterObject CarouselPageQuery query,
            BindingResult br
    ) {
        return br.hasErrors() ? R.ok(PageVo.instance(CarouselPageVo.class)) : R.ok(carouselService.getPage(query));
    }


    @Operation(summary = "添加轮播图", description = "添加轮播图")
    @PostMapping
    @SaCheckPermission("platform:carousel:add")
    public R<Object> add(
            @Validated @RequestBody CarouselAddQuery query
    ) {
        carouselService.add(query);
        return R.ok();
    }

    @Operation(summary = "更新轮播图", description = "更新轮播图")
    @SaCheckPermission("platform:carousel:update")
    @PutMapping
    public R<Object> updateById(
            @Validated @RequestBody CarouselUpdateQuery query
    ) {
        carouselService.updateById(query);
        return R.ok();
    }

    @Operation(summary = "删除轮播图", description = "删除轮播图")
    @SaCheckPermission("platform:carousel:delete")
    @DeleteMapping({"{id}"})
    public R<Object> deleteById(
            @Schema(description = "轮播图ID") @NotNull @Min(value = 1)
            @PathVariable @Min(1) Long id
    ) {
        carouselService.deleteById(id);
        return R.ok();
    }


    @Operation(summary = "批量删除轮播图", description = "批量删除轮播图")
    @SaCheckPermission("platform:carousel:delete")
    @DeleteMapping
    public R<Object> deleteBatchByIdSet(
            @Schema(description = "轮播图ID列表") @NotNull @NotEmpty Set<Long> idList
    ) {
        carouselService.deleteBatchIdSet(idList);
        return R.ok();
    }


    @Operation(summary = "上传轮播图", description = "上传轮播图")
    @SaCheckPermission("platform:carousel:upload")
    @PostMapping({"upload"})
    public R<String> upload(MultipartFile file) {
        return R.ok(carouselService.upload(file));
    }

    @Operation(summary = "批量上传轮播图", description = "批量上传轮播图")
    @SaCheckPermission("platform:carousel:multi-upload")
    @PostMapping({"multi-upload"})
    public R<List<String>> multiUpload(List<MultipartFile> fileList) {
        return R.ok(carouselService.multiUpload(fileList));
    }

}
